package 最短补全词;

import java.util.Locale;

public class Solution {
    public static void main(String[] args) {
        Solution s = new Solution();
        String l = "iMSlpe4";
        String[] words = new String[]{"claim","consumer","student","camera","public","never","wonder","simple","thought","use"
 };
        System.out.println(s.shortestCompletingWord(l,words));
    }

    public String shortestCompletingWord(String licensePlate, String[] words) {
        int[] chars = new int[26];
        licensePlate = licensePlate.toLowerCase();
        for (int i = 0; i < licensePlate.length();i++){
            char c = licensePlate.charAt(i);

            if(c - 'a' >= 0){
                int index = c - 'a';
                chars[index]++;
            }

        }

        int resultIndex = -1;
        int lenth = -1;
        outer:for (int index = 0;index < words.length;index++) {
            String word = words[index].toLowerCase();
            int[] chars2 = new int[26];
            for (int i = 0; i < word.length();i++){
                char c = word.charAt(i);
                if(c - 'a' >= 0){
                    chars2[c - 'a']++;
                }

            }
            for (int j = 0; j < 26; j++) {
                if(chars[j] > chars2[j]){
                    continue outer;
                }
            }
            if(word.length() < lenth || lenth == -1){
                resultIndex = index;
                lenth = word.length();
            }

        }

        return words[resultIndex];
    }
}
